package com.example.xmind2testcase.controller;

import com.example.xmind2testcase.exportdata.TestCaseExportData;
import com.example.xmind2testcase.pojo.Testcase;
import com.example.xmind2testcase.pojo.TestplainTestcase;
import com.example.xmind2testcase.pojo.vo.CaseVo;
import com.example.xmind2testcase.service.TestcaseManageService;
import com.example.xmind2testcase.util.ExcelUtil;
import com.example.xmind2testcase.util.R;
import com.example.xmind2testcase.vo.TestcaseVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
public class TestcaseManageController {

    @Autowired
    private TestcaseManageService testcaseManageService;


    @PostMapping("/testcase/save")
    public R saveTestcase(@RequestBody TestcaseVo testcaseVo) {
        String s = testcaseManageService.saveTestcase(testcaseVo);
        return R.ok(s);
    }

    @GetMapping("/testcase/get/{fileId}")
    public R getTestcase(@PathVariable("fileId") Long fileID) {
        TestcaseVo testcase = testcaseManageService.getTestcase(fileID);
        return R.ok(testcase);
    }

    @GetMapping("/testcase/translateXmind2Test/{fileId}")
    public R translateXmind2Test(@PathVariable("fileId") Long fileID) {
        ArrayList<HashMap<String, Object>> stringArrayListHashMap = testcaseManageService.translateXmind2Testcase(fileID);
        return R.ok(stringArrayListHashMap);
    }


    @GetMapping("/testcase/download/{fileId}")
    public void downloadXmind(@PathVariable("fileId") long fileId, HttpServletResponse response) {
        testcaseManageService.downloadXmind(fileId, response);
    }

    @PostMapping("/testcase/saveDesc")
    public R saveTestcaseDesc(@RequestBody List<Testcase> testcaseList) {
        testcaseManageService.updateTestcase(testcaseList);
        return R.ok("更新成功");
    }

    @PostMapping("/testplaincase/addtestcase/{plainId}/{fileId}")
    public R addTestcase2Testplain(@RequestBody List<String> testcaseList, @PathVariable("plainId") Long plainId, @PathVariable("fileId") Long fileId) {
        testcaseManageService.add2TestPlain(testcaseList, plainId, fileId);
        return R.ok("添加成功");
    }

    @GetMapping("/testplaincase/gettestcaseplain/{plainId}")
    public R getTestcasePlain(@PathVariable("plainId") Long plainId) {
        List<TestplainTestcase> testcasePlain = testcaseManageService.getTestcasePlain(plainId);
        return R.ok(testcasePlain);
    }

    @PostMapping("/testplaincase/gettestcaseplain/exporttestcaseexcel")
    public void exportTestcaseExcel(@RequestBody List<CaseVo> testcaseVos, HttpServletResponse response) throws JsonProcessingException {
        List<TestCaseExportData> testCaseExportData = testcaseManageService.exportTestcaseExcel(testcaseVos);
        ExcelUtil.exportExcel(testCaseExportData, "测试用例", "测试用例", TestCaseExportData.class
                , "测试用例", response);

    }

    @PostMapping("/testcase/uploadFile")
    public R uploadFile(MultipartFile file) {
        testcaseManageService.uploadFile(file);
        return R.ok();
    }

    @GetMapping("/testcase/deleteFile/{fileId}")
    public R deleteFile(@PathVariable("fileId") Long fileId){
        String message = testcaseManageService.deleteFile(fileId);
        return R.ok(message);
    }
}
